package com.imooc.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imooc.dao.GirlRepository;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.GirlException;
import com.imooc.pojo.Girl;

@Service
public class GirlService {
	
	@Autowired
	private GirlRepository girlRepository;

	@Transactional
	public void insertTwo(){
		Girl girlA = new Girl();
		girlA.setCupSize("A");
		girlA.setAge(19);
		girlRepository.save(girlA);
		
		Girl girlB = new Girl();
		girlB.setCupSize("E");
		girlB.setAge(19);
		girlRepository.save(girlB);
	}
	
	/**
	 * 统一异常处理
	* @Title: getAge 
	* @Description: TODO
	* @param @param id
	* @param @throws Exception 设定文件 
	* @return 返回类型 
	* @throws
	 */
	@Transactional
	public void getAge(Integer id) throws Exception{
		Girl girl = girlRepository.findOne(id);
		Integer age = girl.getAge();
		if(age < 10 ){
			throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
		}else if(age > 10 && age <16) {
			throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
		}else if(age >= 16 && age <= 38){
			throw new GirlException(ResultEnum.DAXIONG_GIRL);
		}else{
			throw new GirlException(ResultEnum.LADAO);
		}
	}
	
	/**
	 * 根据id查询一个女生信息
	* @Title: findOne 
	* @Description: TODO
	* @param @param id
	* @param @return 设定文件 
	* @return 返回类型 
	* @throws
	 */
	public Girl findOne(Integer id){
		return girlRepository.findOne(id);
	}
}
